<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleCopyAdd"
        >复制新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleBatchDelete"
        >批量删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单体类型" align="center" :show-overflow-tooltip="true">
         <template slot-scope="scope">
           <router-link :to="'/system/cell-reflection-list/index/' + scope.row.id" class="link-type">
             <span>{{ scope.row.cellTypeName }}</span>
           </router-link>
         </template>
       </el-table-column>
      <el-table-column label="描述前缀" align="center" prop="descriptionPrefix"/>
      <el-table-column label="类型数量" align="center" prop="cellTypeNum"/>
      <el-table-column label="类型前缀" align="center" prop="cellTypePrefix"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDeleteReflection(scope.row)"
          >删除映射</el-button>

        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场站ID">
          <el-input v-model="this.stationId" :disabled="true" />
        </el-form-item>
        <el-form-item label="单体类型" prop="cellType">
          <el-select
            v-model="form.cellType"
            placeholder="单体类型"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.cell_type_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述前缀" prop="descriptionPrefix">
          <el-input v-model="form.descriptionPrefix" type="text" placeholder="请输入字段描述前缀" />
        </el-form-item>
        <el-form-item label="类型数量" prop="cellTypeNum">
          <el-input v-model="form.cellTypeNum" type="number" min="0" placeholder="请输入单体类型数量" />
        </el-form-item>
        <el-form-item label="类型前缀" prop="cellTypePrefix">
          <el-input v-model="form.cellTypePrefix" type="text" placeholder="请输入单体类型前缀" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getCellTypeDetail,listCellType,addCellType,updateCellType,delCellType,batchUpdateCellType,batchDelCellType }
  from "@/api/system/station/cell_type";
import { getStationDetail }
  from "@/api/system/station/station";
import {delCellReflection} from "@/api/system/station/cell_reflection";


export default {
  name: "Data",
  dicts: ['cell_type_list'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dataList: [],
      stationId : '',
      stationName:'',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 表单参数
      form: {
        id:'',
        stationId:'',
        cellType:'',
        descriptionPrefix:'',
        cellTypeNum:'',
        cellTypePrefix:''
      },
      // 表单校验
      rules: {
        cellType: [
          {required: true, message: "单体类型不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    const stationCode = this.$route.params.stationCode;
    this.getStationInfo(stationCode);
    this.loading = false;
  },
  methods: {
    /** 查询场站详情 */
    getStationInfo(stationCode) {
      getStationDetail(stationCode).then(response => {
        this.stationId = response.data.stationId;
        this.form.stationId = response.data.stationId;
        this.stationName = response.data.stationName;
        this.getList();
      });
    },
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      listCellType(this.stationId).then(response => {
        this.dataList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: '',
        station_id:'',
        cellType:'',
        description : '',
        cellTypeNum:'',
        cellTypePrefix:''
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: "/system/station" };
      this.$tab.closeOpenPage(obj);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dictType = this.defaultDictType;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单体点表数据";
    },
    /** 新增按钮操作 */
    handleCopyAdd() {
      const ids = this.ids;
      if (ids.length > 1){
        this.$modal.msgWarning("只能选择一个");
        return;
      }
      const id = ids[0];
      this.disabled = false;
      this.reset();
      getCellTypeDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "添加单体类型数据";
        this.form.id = undefined;
      });

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id
      getCellTypeDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "添加单体类型数据";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCellType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCellType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除单体为"' + row.cellTypeName + '"的数据项？').then(function() {
        return delCellType(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleBatchDelete() {
      const ids = this.ids;
      this.$modal.confirm('是否确认删除单体为"' + ids + '"的数据项？').then(function() {
        return batchDelCellType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleDeleteReflection(row) {
      this.$modal.confirm('是否确认删除单体类型为"' + row.cellTypeName + '"的映射数据项？').then(function() {
        return delCellReflection(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }

};
</script>
